package com.asia.algorithmcode.linkedList;

/**
 * @DESCRIPTION:
 * @USER: wanfu
 * @DATE: 2025/3/11 星期二 16:34
 */
public class RotateRight {

    public ListNode rotateRight(ListNode head, int k) {
        ListNode tail = head;
        while (tail.next.next != null) {
            tail = tail.next;
        }

        while (k > 0) {
            tail.next.next = head;
            head = tail.next;
            tail.next = null;

            ListNode temp = head;
            while (temp.next.next != null) {
                temp = temp.next;
            }
            tail = temp;
            k--;
        }
        return head;
    }


    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        n1.next = n2;
        n2.next = n3;


        RotateRight rotateRight = new RotateRight();
        ListNode ans = rotateRight.rotateRight(n1, 2);
        System.out.println(ans);

    }


    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
